package com.xcity.game.activity2.groupbuy.template;

import java.util.ArrayList;
import java.util.List;

import naga.x.game.template.Template;
import naga.x.game.template.TemplateException;
import naga.x.game.template.annotation.Excel;

/**
 * 限时团购商品表
 * @author yang.li
 *
 */
@Excel(value = "group_buy.xlsx", sheet = 0)
public class GroupBuyTemplate extends Template {

	/** 物品id */
	protected int itemId;

	/** 物品数量 */
	protected int count;

	/** 每人限购次数 */
	protected int numLimit;

	/** 团购券限用数量 */
	protected int couponLimit;

	/** 原价（钻石） */
	protected int price;

	/** 折扣 */
	protected float[] discounts;
	protected List<Discount> discountsWrapped;

	public int getItemId() {
		return itemId;
	}

	public void setItemId(int itemId) {
		this.itemId = itemId;
	}

	public int getCount() {
		return count;
	}

	public void setCount(int count) {
		this.count = count;
	}

	public int getNumLimit() {
		return numLimit;
	}

	public void setNumLimit(int numLimit) {
		this.numLimit = numLimit;
	}

	public int getCouponLimit() {
		return couponLimit;
	}

	public void setCouponLimit(int couponLimit) {
		this.couponLimit = couponLimit;
	}

	public int getPrice() {
		return price;
	}

	public void setPrice(int price) {
		this.price = price;
	}

	public float[] getDiscounts() {
		return discounts;
	}

	public void setDiscounts(float[] discounts) {
		this.discounts = discounts;
	}

	public Discount getDiscount(int soldNum) {
		if (soldNum <= 0) {
			return discountsWrapped.get(0);
		}
		for (int i = discountsWrapped.size() - 1; i >= 0; i--) {
			Discount d = discountsWrapped.get(i);
			if (soldNum >= d.numRequire) {
				return d;
			}
		}
		return discountsWrapped.get(0);
	}
	
	public List<Discount> getDiscountsWrapped() {
		return discountsWrapped;
	}
	
	public List<Discount> getDiscountsWithoutFullPrice() {
		return discountsWrapped.subList(1, discountsWrapped.size());
	}

	@Override
	public void validate() throws TemplateException {
		discountsWrapped = new ArrayList<>(discounts.length / 3 + 1);
		// 10折（全价）
		Discount d = new Discount();
		d.discount = 10;
		d.price = price;
		discountsWrapped.add(d);
		for (int i = 0; i < discounts.length; i += 3) {
			d = new Discount();
			d.discount = discounts[i];
			d.numRequire = (int) discounts[i + 1];
			d.price = (int) discounts[i + 2];
			discountsWrapped.add(d);
		}
		discounts = null;
	}
	
	public static class Discount {
		// 折扣
		public float discount;
		// 该折扣需售件数
		public int numRequire;
		// 折扣价
		public int price;
	}
}
